import {
  ArgumentMetadata,
  Injectable,
  PipeTransform,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { Validator, isUUID } from 'class-validator';

@Injectable()
export class ParsePrimarykey implements PipeTransform {
  public validator: Validator;
  constructor() {
    this.validator = new Validator();
  }

  transform(value: string, metadata: ArgumentMetadata): string {
    if (!isNaN(parseInt(value, 10)) || isUUID(value)) {
      return value;
    } else {
      throw new HttpException(`传递的id必须是整形或者是uuid字符串,你传递的是:${value}`, HttpStatus.BAD_REQUEST);
    }
  }
}
